import datetime
from applications.extensions import db
from sqlalchemy import UniqueConstraint

class StoreRawYPModel(db.Model):
    __tablename__ = '原料_yp_入库'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    raw_id = db.Column(db.Integer,db.ForeignKey("商品档案_原料_饮片.id"), comment='原料_编号', nullable=False) # 属性 外键
    supplier_id = db.Column(db.Integer,db.ForeignKey("供应商档案_原料_饮片.id"),  comment='供应商_编号', nullable=True)

    store_number = db.Column(db.Numeric(16,4), comment='入库数量 保留小数点后4位',nullable=False)
    price = db.Column(db.Numeric(16, 2), comment='单价 保留小数点后2位', nullable=False)
    residual_number = db.Column(db.Numeric(16,4), comment='剩余数量 保留小数点后4位',nullable=False)
    unit_id = db.Column(db.Integer,db.ForeignKey("admin_dict_data.id"),  comment='数据字典 单位_编号', nullable=False)
    type = db.Column(db.String(255), default='入库单', comment='入库单|期初入库单|其他入库单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    产地 = db.Column(db.String(255),  comment='产地',nullable=False)
    规格 = db.Column(db.String(255), comment='规格', nullable=False)
    批号 = db.Column(db.String(255), comment='批号', nullable=False)
    user_buyer_id= db.Column(db.Integer,db.ForeignKey("admin_user.id"),  comment='采购人员_编号', nullable=True)
    user_keeper_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='库管人员_编号', nullable=False)
    buyer_time = db.Column(db.Date,  comment='采购日期', nullable=True)
    user_qa_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='质量人员_编号', nullable=True)
    qa_time = db.Column(db.Date,  comment='验收日期', nullable=True)
    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')
    files = db.relationship('StoreRawYPFilesModel', backref=db.backref('storeRawYp'),lazy='dynamic')
    # location = db.relationship('StoreRawYPModel_location', backref=db.backref('storeRawYp'), lazy='dynamic')
    raw = db.relationship('RawYPModel', backref=db.backref('storeRawYp_'),uselist=False) #uselist=False 标识一对一关系
    supplier = db.relationship('SupplierRawYPModel', backref=db.backref('storeRawYp2'),uselist=False)
    unit = db.relationship('DictData', backref=db.backref('storeRawYp'),uselist=False)
    user_buyer = db.relationship('User',foreign_keys=[user_buyer_id], backref=db.backref('storeRawYp_buyer'),uselist=False)
    user_keeper = db.relationship('User', foreign_keys=[user_keeper_id], backref=db.backref('storeRawYp_keeper'),uselist=False)
    user_qa = db.relationship('User',foreign_keys=[user_qa_id], backref=db.backref('storeRawYp'),uselist=False)

    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}

class StoreRawYPModel_location(db.Model):
    __tablename__ = '原料_yp_按货位入库'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    store_id = db.Column(db.Integer,db.ForeignKey("原料_yp_入库.id"), comment='入库_编号', nullable=False) # 属性 外键
    type = db.Column(db.String(255), default='入库单', comment='入库单|期初入库单|调拨单')
    store_number = db.Column(db.Numeric(16,4), comment='入库数量 保留小数点后4位',nullable=False)
    stock_number = db.Column(db.Numeric(16,4), comment='库存数量 保留小数点后4位',nullable=False)
    location_id = db.Column(db.Integer,db.ForeignKey("货位管理_yp_原料.id"), comment='货位_编号', nullable=False) # 属性 外键
    parent_id = db.Column(db.Integer,db.ForeignKey("原料_yp_按货位入库.id"), comment='调拨_编号',default=None, nullable=True) # 属性 外键

    location = db.relationship('LocationRawYP', backref=db.backref('storeRawYp_l'), uselist=False)
    storeRawYp = db.relationship('StoreRawYPModel', backref=db.backref('location'), uselist=False)
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}

class StoreRawYPFilesModel(db.Model):
    __tablename__ = '原料_yp_入库_附件'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    data_label = db.Column(db.String(255), nullable=False, comment='文件名称')
    href = db.Column(db.String(255), comment='文件路由路径')
    mime = db.Column(db.CHAR(255), nullable=False, comment='文件类型')
    size = db.Column(db.CHAR(30), nullable=False, comment='文件大小')

    description = db.Column(db.String(255),  comment='描述')
    create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                            comment='更新时间')
    Store_raw_id = db.Column(db.Integer,db.ForeignKey("原料_yp_入库.id"), comment='入库_编号')

    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}



#入库退货
class StoreRawYPReturnSupplierModel(db.Model):
    __tablename__ = '原料_yp_入库_退货'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    return_dh = db.Column(db.String(255), comment='退货单号', nullable=False)
    location_id = db.Column(db.Integer,db.ForeignKey("原料_yp_按货位入库.id"), comment='入库_编号', nullable=False) # 属性 外键
    return_number = db.Column(db.Numeric(16,4), comment='退货数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='退货单', comment='退货单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_keeper_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='库管人员_编号', nullable=False)

    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    location = db.relationship('StoreRawYPModel_location', backref=db.backref('storeRawYpReturnSupplier'), uselist=False)
    user_keeper = db.relationship('User', foreign_keys=[user_keeper_id], backref=db.backref('storeRawYpReturnSupplier_keeper'),uselist=False)


    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}

#报损
class StoreRawYPLossModel(db.Model):
    __tablename__ = '原料_yp_入库_报损'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    loss_dh = db.Column(db.String(255), comment='报损单号', nullable=False)
    location_id = db.Column(db.Integer,db.ForeignKey("原料_yp_按货位入库.id"), comment='入库_编号', nullable=False) # 属性 外键
    loss_number = db.Column(db.Numeric(16,4), comment='报损数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='报损单', comment='报损单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_keeper_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='库管人员_编号', nullable=False)

    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    location = db.relationship('StoreRawYPModel_location', backref=db.backref('storeRawYpLoss'), uselist=False)
    user_keeper = db.relationship('User', foreign_keys=[user_keeper_id], backref=db.backref('storeRawYpLoss_keeper'),uselist=False)


    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}


#报溢
class StoreRawYPExcessModel(db.Model):
    __tablename__ = '原料_yp_入库_报溢'
    id = db.Column(db.Integer, primary_key=True, comment='ID') #默认自增primary_key
    excess_dh = db.Column(db.String(255), comment='报溢单号', nullable=False)
    location_id = db.Column(db.Integer,db.ForeignKey("原料_yp_按货位入库.id"), comment='入库_编号', nullable=False) # 属性 外键
    excess_number = db.Column(db.Numeric(16,4), comment='报溢数量 保留小数点后4位',nullable=False)
    type = db.Column(db.String(255), default='报溢单', comment='报溢单')
    date_time = db.Column(db.Date, default=datetime.date.today(), comment='单据日期')
    user_keeper_id = db.Column(db.Integer, db.ForeignKey("admin_user.id"), comment='库管人员_编号', nullable=False)

    remark = db.Column(db.String(255), comment='备注')
    create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                          comment='更新时间')

    location = db.relationship('StoreRawYPModel_location', backref=db.backref('storeRawYpExcess'), uselist=False)
    user_keeper = db.relationship('User', foreign_keys=[user_keeper_id], backref=db.backref('storeRawYpExcess_keeper'),uselist=False)


    # #创建联合索引
    # __table_args__ = (
    #     UniqueConstraint('代码', '版本号', name='unix_raw_yp_code_version'),
    # )
    def to_dict(self):
        return {column.name: getattr(self, column.name) for column in self.__table__.columns}